<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\api\model\user\UserCompanyAuth as UserCompanyAuthModel;

/**
 * 单位认证接口
 */
class Usercompanyauth extends Api
{
    protected $noNeedRight = ['*'];

    //提交认证
    public function add()
    {

        $person_auth = \app\api\model\user\UserPersonAuth::get(['user_id'=>$this->auth->id]);
        if ($person_auth){
            $this->error('已是个人会员');
        }


        $model = new UserCompanyAuthModel();

        $info = UserCompanyAuthModel::get(['user_id'=>$this->auth->id]);
        /*if ($info){
            $this->error('已是单位会员');
        }*/
        $data = [
            'user_id'=>$this->auth->id,
            'company_name'=>input('company_name',''),
            'business_licence_number'=>input('business_licence_number',''),
            'company_found_date'=>input('company_found_date',''),
            'company_address'=>input('company_address',''),
            'company_attr'=>input('company_attr',''),
            'company_industry'=>input('company_industry',''),
            'company_business'=>input('company_business',''),
            'company_people_num'=>input('company_people_num',0),
            'company_mobile'=>input('company_mobile',''),
            'company_intro'=>input('company_intro',''),
            'status'=>1
        ];
        if (!$info){
            $exist = UserCompanyAuthModel::get(['business_licence_number'=>$data['business_licence_number']]);
            if ($exist)$this->error('当前统一社会信用代码已存在');
        } else {
            $exist = UserCompanyAuthModel::where(['business_licence_number'=>$data['business_licence_number']])
                ->where('id','<>',$info['id'])->find();
            if ($exist)$this->error('当前统一社会信用代码已存在');
        }

        if (empty($data['company_name']))$this->error('请输入单位名称');
        if (empty($data['business_licence_number']))$this->error('请输入统一社会信用代码');
        if (empty($data['company_found_date']))$this->error('请输入成立日期');
        if (empty($data['company_address']))$this->error('请输入单位地址');
        if (empty($data['company_attr']))$this->error('请输入单位属性');
        if (empty($data['company_industry']))$this->error('请输入所属行业');
        if (empty($data['company_business']))$this->error('请输入主营业务');
        if (empty($data['company_people_num']))$this->error('请输入成员人数');
        if (empty($data['company_mobile']))$this->error('请输入联系电话');
        if (empty($data['company_intro']))$this->error('请输入单位介绍');

        if (!$info){
            $res = $model->save($data);
        } else {
            $res = $model->save($data,['id'=>$info['id']]);
        }
        $user = \app\admin\model\User::get($data['user_id']);
        $user->company_name = $model->company_name;
        $user->business_licence_number = $model->business_licence_number;
        $user->company_found_date = $model->company_found_date;
        $user->company_address = $model->company_address;
        $user->company_attr = $model->company_attr;
        $user->company_industry = $model->company_industry;
        $user->company_business = $model->company_business;
        $user->company_people_num = $model->company_people_num;
        $user->company_mobile = $model->company_mobile;
        $user->company_intro = $model->company_intro;
        /*if ($user->auth_status==2){
            $user->level_type = 4;
            $user->level = 1;
        }*/
        $user->company_auth = 1;
        $user->save();
        $this->success('提交成功',$res);
    }

    //认证信息
    public function info()
    {
        $info = UserCompanyAuthModel::get(['user_id'=>$this->auth->id]);
        $this->success('',$info);
    }
}